﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SqlServerDataTableToExcel
{
    public partial class frmFileEdit : Form
    {

        public Model.Item_Model objItem { get; set; }
       
        public frmFileEdit(Model.Item_Model _objItem)
        {
            InitializeComponent();

            foreach (Model.DataTypeEnum item in Enum.GetValues(typeof(Model.DataTypeEnum)))
            {
                cbxType.Items.Add(item.ToString());
            }

            
            if (_objItem != null)
            {
                objItem = _objItem;

                tbxName.Text = objItem.Name;
                tbxIP.Text = objItem.IpAddress;
                tbxUserName.Text = objItem.Ds_Name;
                tbxUserPwd.Text = objItem.Ds_Pwd;
                cbxType.Text = objItem.Type;
                cbxDataName.Text = objItem.DataSource;
            }
            else
            {
                objItem = new Model.Item_Model();
                objItem.guid = Guid.NewGuid().ToString();
                objItem.FileName = "";
            }

           
        }

        private void btnConnect_Click(object sender, EventArgs e)
        {
            try
            {
                string IPAddress = tbxIP.Text;
                string UserName = tbxUserName.Text;
                string UserPwd = tbxUserPwd.Text;
                string Type = cbxType.Text.ToString();

                BLL.FrmFuc objFrmFuc = new BLL.FrmFuc(Type);
                List<string> dsList = objFrmFuc.GetDataSourceList( IPAddress, UserName, UserPwd);
                cbxDataName.DataSource = dsList;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            try
            {
                if (cbxDataName.Text == "")
                {
                    throw new Exception("请选择数据库");
                }

                string name = tbxName.Text;
                string IPAddress = tbxIP.Text;
                string UserName = tbxUserName.Text;
                string UserPwd = tbxUserPwd.Text;
                string Type = cbxType.SelectedItem.ToString();
                string DsName = cbxDataName.Text;
                //AppConfig.DataSourceName = cbxDataName.SelectedValue.ToString();

                //AppConfig.SaveConfigValueByKey("FileName", tbxfileName.Text);
                //AppConfig.SaveConfigValueByKey("IP", tbxIP.Text);
                //AppConfig.SaveConfigValueByKey("PWD", AppConfig.DESSymmetric.Encrypt(tbxsa.Text));

                //List<MODEL.DataTableMsg> tableMsg = BLL.GetTableMsg();

                //BLL.ExportToExcel(tableMsg, tbxfileName.Text);
                //MessageBox.Show("OK!");

                objItem.Name = tbxName.Text;
                objItem.Type = Type;

                objItem.IpAddress = IPAddress;
                objItem.DataSource = DsName;
                objItem.Ds_Name = UserName;
                objItem.Ds_Pwd = UserPwd;

                BLL.LocalDs.Save(objItem);

                this.DialogResult = DialogResult.OK;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void tbxName_TextChanged(object sender, EventArgs e)
        {

        }
    }
}
